package com.wteam.dragon.system.apps.server.controller;

import com.wteam.dragon.system.apps.server.povo.bean.Email;
import com.wteam.dragon.system.apps.server.service.EmailService;
import com.wteam.dragon.system.apps.server.povo.vo.EmailVo;
import com.wteam.dragon.system.apps.server.povo.vo.SendEmailVo;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 陈晓辉
 * @Date 2020/3/24
 */
@RestController
@Api(tags = "系统：邮箱")
@RequestMapping("admin/email")
@RequiredArgsConstructor
public class EmailController {
    private final EmailService emailService;

    @ApiOperation("添加邮箱")
    @PostMapping("addEmail")
    @PreAuthorize("@el.check('addEmail')")
    public ResultMessage addEmail(EmailVo emailVo){
        Email email = BeanUtil.from(emailVo, Email.class);
        return emailService.addEmail(email);
    }

    @ApiOperation("删除邮箱")
    @DeleteMapping("delEmail")
    @PreAuthorize("@el.check('delEmail')")
    public ResultMessage delEmail(Long id){
        return emailService.deleteEmail(id);
    }

    @ApiOperation("更新邮箱")
    @PutMapping("editEmail")
    @PreAuthorize("@el.check('editEmail')")
    public ResultMessage editEmail(EmailVo emailVo){
        Email email = BeanUtil.from(emailVo, Email.class);
        return emailService.updateEmail(email);
    }

    @ApiOperation("获得邮箱列表")
    @GetMapping("listEmail")
    @PreAuthorize("@el.check('listEmail')")
    public ResultMessage listEmail(){
        return emailService.getEmailList();
    }

    @ApiOperation("发送邮件")
    @PutMapping("sendEmail")
    @PreAuthorize("@el.check('sendEmail')")
    public ResultMessage sendEmail(EmailVo emailVo, SendEmailVo sendEmailVo){
        Email email = BeanUtil.from(emailVo, Email.class);
        return emailService.sendEmail(email, sendEmailVo);
    }
}
